const { getCarouselList, queryCarousel, delCarousel, addCarousel, findCarouselOfId, editCarousel } = require("../models/carouselModel")

module.exports = {
  //获取轮播图列表
  getCarouselList: async (ctx) => {
    //获取轮播图列表
    let res = await getCarouselList();

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
        data: []
      }
    }
  },
  // 通过时间查询轮播图列表
  queryCarousel: async (ctx) => {
    const { start_time, end_time } = ctx.request.query;

    // 通过时间查询轮播图列表
    let res = await queryCarousel(start_time, end_time);
    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "查询成功",
        data: res
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "此时间段暂无数据",
        data: []
      }
    }
  },
  // 删除轮播图
  delCarousel: async (ctx) => {
    const { id } = ctx.request.body;
    // 删除轮播图
    let res = await delCarousel(id);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "删除成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "删除失败"
      }
    }
  },
  // 添加轮播图
  addCarousel: async (ctx) => {
    const { status, operate_place, start_time, end_time, swipe_image } = ctx.request.body;
    // 添加轮播图
    let res = await addCarousel(status, operate_place, start_time, end_time, swipe_image);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "添加成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "添加失败"
      }
    }
  },
  // 编辑回显
  editEcho: async (ctx) => {
    const { id } = ctx.request.query;

    // 删除用户
    let res = await findCarouselOfId(id);

    if (res.length === 0) {
      return ctx.body = {
        status: 205,
        msg: "轮播图数据不存在"
      }
    }
    ctx.body = {
      status: 200,
      msg: "查询成功",
      data: res[0]
    }
  },
  // 编辑用户
  editCarousel: async (ctx) => {
    const { id, status, operate_place, start_time, end_time, swipe_image } = ctx.request.body;
    let res = await editCarousel(id, status, operate_place, start_time, end_time, swipe_image);

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "编辑成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "编辑失败"
      }
    }
  }
}